package korkd.tower;

import org.anddev.andengine.engine.handler.IUpdateHandler;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.opengl.texture.region.TextureRegion;

class Bullet extends Sprite
{
	// Attributes
	private int count;
	private int mVelocity;
	private int mDamage;
	
	// Constructor
	public Bullet(float pX, float pY, TextureRegion pTextureRegion) {
		super(pX, pY, pTextureRegion);
		mDamage = 60;
		mVelocity = 15;
	}
	
	public void travel(final Unit u, final BulletPool pBulletPool)
	{
		final Bullet b = this;
		this.registerUpdateHandler(new IUpdateHandler() {
			@Override
			public void onUpdate(float pSecondsElapsed)
			{
				double angle = Math.atan2(u.getY()-b.getY(), u.getX()-b.getX());
				b.setPosition(b.getX()+(float)(mVelocity*Math.cos(angle)), b.getY()+(float)(mVelocity*Math.sin(angle)));

				if(b.collidesWith(u))
				{
					u.hit(mDamage);
					b.unregisterUpdateHandler(this);
					pBulletPool.recyclePoolItem(b);
				}
			}

			@Override
			public void reset() {
				// TODO Auto-generated method stub
				
			}
		});
	}
	
	
	// BEGIN Setters and Getters
	// ****************************************************************

	public void setCount(int count) {
		this.count = count;
	}

	public int getCount() {
		return count;
	}

	
}